﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Entities;
using VitCode.Castle.Helper;
using VitCode.Web.UI.WebControls;

namespace AMS.Web.BackStageManagement
{
    public partial class PassUpdate : Page
    { 
        CastleAccess acc = new CastleAccess();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies["Code"] == null)
            {
                Response.Redirect("../Default.aspx");
            }

            if (!IsPostBack)
            {
                if (Request.QueryString["Id"] != null)
                {
                    labelId.Text = Request.QueryString["Id"].Trim();

                    var objUser = acc.GetObjectList(new User().GetType().Name, "Id", labelId.Text.Trim());
                    if (objUser != null && objUser.Count > 0)
                    {
                        User user = (User) objUser[0];
                        ZLTextBox_AdminName.Text = user.Code;
                        ZLTextBox_AdminRealName.Text = user.Name;
                       

                    }
                }
            }
        }
      protected void btnSubmit_Click(object sender, EventArgs e)
        {
            acc.BeginTransaction();
            try
            {
                var objUser = acc.GetObjectList(new User().GetType().Name, "Id", labelId.Text.Trim());
                if (objUser != null && objUser.Count > 0)
                {
                    var user = (User)objUser[0];
                    string strOldPassword = user.Password;
                    if (strOldPassword != publ.VitEnctyptString(ZLTextBox_OldPassword.Text.Trim()))
                    {
                        Alert.MessageBox("旧密码不正确!");
                        return;
                    }
                    string strCode = ZLTextBox_AdminName.Text.Trim();
                    string strName = ZLTextBox_AdminRealName.Text.Trim();
                    string strPassword = publ.VitEnctyptString(ZLTextBox_AdminPass.Text.Trim());
                    user.Code = strCode;
                    user.Name = strName;
                    user.Password = strPassword;

                    acc.SaveOrUpdate(user);
                    acc.Commit();
                    Alert.MessageBox("修改成功!");

                }
            }
            catch (Exception)
            {
                acc.RollBack();
                throw;
            }
            finally
            {
                acc.CloseSession();
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Window.Close();
        }
    }
}
